package com.mzpai.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mzpai.product.model.StoreBrand;
import com.mzpai.product.model.StoreCategory;
import com.mzpai.product.model.StoreProduct;
import com.mzpai.product.model.StoreProductDetailsSupplier;
import com.mzpai.product.model.StoreProductSupplier;
import com.mzpai.product.model.SystemAttachment;
import com.mzpai.product.service.StoreBrandService;
import com.mzpai.product.service.StoreCategoryService;
import com.mzpai.product.service.StoreProductAttrResultSupplierService;
import com.mzpai.product.service.StoreProductAttrSupplierService;
import com.mzpai.product.service.StoreProductAttrValueSupplierService;
import com.mzpai.product.service.StoreProductCateSupplierService;
import com.mzpai.product.service.StoreProductDescriptionSupplierService;
import com.mzpai.product.service.StoreProductDetailsSupplierService;
import com.mzpai.product.service.StoreProductService;
import com.mzpai.product.service.StoreProductSupplierService;
import com.mzpai.product.service.SystemAttachmentService;
import com.mzpai.util.SplitBatch;
import com.mzpai.util.StringUtils;
import com.mzpai.vo.ResponseVo;
import com.mzpai.web.utils.StorageUploadUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 上线初始化接口
 * @author ASUS
 */

@Api(value = "初始化工具接口")
@RestController
@RequestMapping("/init")
public class InitController extends BaseController{
	
 protected Logger logger = LoggerFactory.getLogger(InitController.class);

 @Autowired
 private StoreBrandService storeBrandService;
 
 @Autowired
 private StoreProductSupplierService storeProductSupplierService;
 
 @Autowired
 private StoreProductService storeProductService;
 
 
 @Autowired
 private StoreCategoryService storeCategoryService;
 
 @Autowired
 private StoreProductCateSupplierService storeProductCateSupplierService;
 
 @Autowired
 private StoreProductAttrSupplierService storeProductAttrSupplierService;
 
 @Autowired
 private StoreProductAttrValueSupplierService storeProductAttrValueSupplierService;
 
 
 @Autowired
 private StoreProductDescriptionSupplierService storeProductDescriptionSupplierService;
 
 @Autowired
 private StoreProductAttrResultSupplierService storeProductAttrResultSupplierService;
 
 @Autowired
 private StoreProductDetailsSupplierService storeProductDetailsSupplierService;
 
 @Autowired
 private SystemAttachmentService systemAttachmentService;
 
 
 @ApiOperation(value = "导入任何数据前先把brandcode为空的赋值")
 @RequestMapping(value = "/brandCodeInit", method = RequestMethod.GET)
 public ResponseVo<String> brandCodeInit() throws Exception{
	 Map<String, Object> map=new HashMap<String,Object>();
	 List<StoreBrand> list=storeBrandService.getList(map);
	 for(int i=0;i<list.size();i++){
		 StoreBrand storeBrand=list.get(i);
		 Integer brandId=storeBrand.getBrandId();
		 String brandCode=storeBrand.getBrandCode();
		 logger.info("当前进行中初始化老品牌brandcode第 "+i+"个 brandid="+storeBrand.getBrandId());
		 if(StringUtils.isNullOrBlank(brandCode)){
			 StoreBrand updatebrand=new StoreBrand();
			 updatebrand.setBrandId(brandId);
			 //生产brandcode
			 String shuijicode=StringUtils.getRandomCode(6);
			 updatebrand.setBrandCode(shuijicode);
	    	 storeBrandService.update(updatebrand);
		 }
	 }   
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	resVo.setResult(result);
 	return resVo;
 }
 
//数据库手动新增分类是store设置为-1 然后执行此方法
@ApiOperation(value = "导入任何数据前先把分类catecode初始化")
@RequestMapping(value = "/cateCodeInit", method = RequestMethod.GET)
public ResponseVo<String> categoryInit() throws Exception{
   String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	Map<String, Object> map=new HashMap<String,Object>();
	List<StoreCategory> list=storeCategoryService.queryList(map);
	for(int i=0;i<list.size();i++){
		StoreCategory storeCategory=list.get(i);
		logger.info("当前进行中初始化分类catecodee第 "+i+"个 brandid="+storeCategory.getId());
		String cateCodestr=storeCategory.getCateCode();
		if(StringUtils.isNullOrBlank(cateCodestr)){
			String cate_Code=StringUtils.getRandomCode(8);
			StoreCategory update=new StoreCategory();
			update.setId(storeCategory.getId());
			update.setCateCode(cate_Code);
			storeCategoryService.update(update);
		}
	}
	resVo.setResult(result);
	return resVo;
}


 
 @ApiOperation(value = "导入任何数据前先把供应商商品productcode为空内数据初始值")
 @RequestMapping(value = "/supplierProductCodenull", method = RequestMethod.GET)
 public ResponseVo<String> supplierProductCodenull() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	List<StoreProductSupplier> list=storeProductSupplierService.getListProductCodeIsNull();
	for(int i=0;i<list.size();i++){
		StoreProductSupplier productSupplier=list.get(i);
		Integer brandId=productSupplier.getBrandId();
		String cateId=productSupplier.getCateId();
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		String cateIdArray[]=cateId.split(",");
		String cateIdStr="";
		
		String brandCodeSupplier=productSupplier.getBrandCode();
		String cateCodeSupplier=productSupplier.getCateCode();
		
		for(int m=0;m<cateIdArray.length;m++){
			String catestr=cateIdArray[m];
			StoreCategory storeCategory=null;
			if(!StringUtils.isNullOrBlank(catestr)){
				storeCategory=storeCategoryService.queryByPrimaryId(Integer.valueOf(catestr));
			}
			if(storeCategory!=null){
				String ss=storeCategory.getCateCode();
				if(!StringUtils.isNullOrBlank(ss)){
					cateIdStr+= ss+",";
				}
			}
		}
		
		if(!StringUtils.isNullOrBlank(cateIdStr)){
			cateIdStr=cateIdStr.substring(0, cateIdStr.lastIndexOf(","));
		}
		
		logger.info("当前进行中初始化商品productcode第 "+i+"个 brandid="+productSupplier.getId());
		Integer id=productSupplier.getId();
		String productCodeStr=productSupplier.getProductCode();
		if(StringUtils.isNullOrBlank(productCodeStr)){
			StoreProductSupplier updateProduct=new StoreProductSupplier();
			updateProduct.setId(id);
			updateProduct.setProductCode(StringUtils.getBusinessCode("SP"));
			if(storeBrand!=null && StringUtils.isNullOrBlank(brandCodeSupplier)){
				updateProduct.setBrandCode(storeBrand.getBrandCode());
			}
			if(!StringUtils.isNullOrBlank(cateIdStr) && StringUtils.isNullOrBlank(cateCodeSupplier)){
				updateProduct.setCateCode(cateIdStr);
			}
			storeProductSupplierService.update(updateProduct);
		}
	}
	resVo.setResult(result);
 	return resVo;
 }
 
 
 
 @ApiOperation(value = "导入任何数据前先把店铺商品productcode为空内数据初始值")
 @RequestMapping(value = "/storeProductCodenull", method = RequestMethod.GET)
 public ResponseVo<String> storeProductCodenull() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	List<StoreProduct> list=storeProductService.getListProductCodeIsNull();
	for(int i=0;i<list.size();i++){
		StoreProduct product=list.get(i);
		logger.info("当前进行中初始化商品productcode第 "+i+"个 brandid="+product.getId());
		Integer id=product.getId();
		String productCodeStr=product.getProductCode();
		
		Integer brandId=product.getBrandId();
		String cateId=product.getCateId();
		
		
		String brandCodeProduct=product.getBrandCode();
		String cateCodeProduct=product.getCateCode();
		
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		
		StoreProduct updateProduct=new StoreProduct();
		updateProduct.setId(id);
		
		if(StringUtils.isNullOrBlank(productCodeStr)){
//			Date d = new Date();
//    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//    		String dateNowStr = sdf.format(d);
//			String shuijicode=StringUtils.getRandomCode(6);
			updateProduct.setProductCode(StringUtils.getBusinessCode("SP"));
		}else{
			updateProduct.setProductCode(productCodeStr);
		}
		
		//如果商品product为空则更新
		if(storeBrand!=null){
			String brandCode=storeBrand.getBrandCode();
			if(!StringUtils.isNullOrBlank(brandCode) && StringUtils.isNullOrBlank(brandCodeProduct)){
				updateProduct.setBrandCode(brandCode);
			}
			
		}
		String cateIdArray[]=cateId.split(",");
		StringBuffer sb=new StringBuffer();
		for(String cateid:cateIdArray){
			StoreCategory storeCategory=null;
			if(!StringUtils.isNullOrBlank(cateid)){
				storeCategory=storeCategoryService.queryByPrimaryId(Integer.valueOf(cateid));
			}
			
			if(storeCategory!=null){
				String cateCode=storeCategory.getCateCode();
				if(!StringUtils.isNullOrBlank(cateCode)&& StringUtils.isNullOrBlank(cateCodeProduct)){
					sb.append(cateCode+",");
				}
			}
		}
		String catestr=sb.toString();
		if(!StringUtils.isNullOrBlank(catestr)&& catestr.contains(",")){
			catestr=catestr.substring(0, catestr.lastIndexOf(","));
			updateProduct.setCateCode(catestr);
		}
		storeProductService.update(updateProduct);
	}
	resVo.setResult(result);
 	return resVo;
 }
 
 
 
 
 @ApiOperation(value = "将storeprodeuc根据brandcateid设catecode设置值")
 @RequestMapping(value = "/storeProductbrandcateidnull", method = RequestMethod.GET)
 public ResponseVo<String> storeProductbrandcateidnull() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	Map<String,Object> map=new HashMap<String,Object>();
	List<StoreProduct> list=storeProductService.getList(map);
	for(int i=0;i<list.size();i++){
		StoreProduct product=list.get(i);
		logger.info("当前进行中初始化商品productcode第 "+i+"个 brandid="+product.getId());
		Integer id=product.getId();
		String productCodeStr=product.getProductCode();
		String brandCodeSupplier=product.getBrandCode();
		String cateCodeSupplier=product.getCateCode();
		
		Integer brandId=product.getBrandId();
		String cateId=product.getCateId();
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		
		StoreProduct updateProduct=new StoreProduct();
		updateProduct.setId(id);
		
		if(StringUtils.isNullOrBlank(productCodeStr)){
//			Date d = new Date();
//    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//    		String dateNowStr = sdf.format(d);
//			String shuijicode=StringUtils.getRandomCode(6);
			updateProduct.setProductCode(StringUtils.getBusinessCode("SP"));
		}else{
			updateProduct.setProductCode(productCodeStr);
		}
		//如果商品product为空则更新
		if(storeBrand!=null){
			String brandCode=storeBrand.getBrandCode();
			if(!StringUtils.isNullOrBlank(brandCode)&& StringUtils.isNullOrBlank(brandCodeSupplier)){
				updateProduct.setBrandCode(brandCode);
			}
		}
		String cateIdArray[]=cateId.split(",");
		StringBuffer sb=new StringBuffer();
		for(String cateid:cateIdArray){
			StoreCategory storeCategory=null;
			if(!StringUtils.isNullOrBlank(cateid)){
				storeCategory=storeCategoryService.queryByPrimaryId(Integer.valueOf(cateid));
			}
			
			if(storeCategory!=null){
				String cateCode=storeCategory.getCateCode();
				if(!StringUtils.isNullOrBlank(cateCode)){
					sb.append(cateCode+",");
				}
			}
		}
		String catestr=sb.toString();
		if(!StringUtils.isNullOrBlank(catestr)&& catestr.contains(",") && StringUtils.isNullOrBlank(cateCodeSupplier)){
			updateProduct.setCateCode(catestr.substring(0, catestr.lastIndexOf(",")+1));
		}
		storeProductService.update(updateProduct);
	}
	resVo.setResult(result);
 	return resVo;
 }
 
 
 @ApiOperation(value = "将storeprodeuc根据brandcateid设catecode设置值去除分类逗号")
 @RequestMapping(value = "/storeProductbrandcateidnulldeldouhao", method = RequestMethod.GET)
 public ResponseVo<String> storeProductbrandcateidnulldeldouhao() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	Map<String,Object> map=new HashMap<String,Object>();
	List<StoreProduct> list=storeProductService.getList(map);
	for(int i=0;i<list.size();i++){
		StoreProduct product=list.get(i);
		logger.info("当前进行中初始化商品productcode第 "+i+"个 brandid="+product.getId());
		Integer id=product.getId();
		String productCodeStr=product.getProductCode();
		String brandCodeSupplier=product.getBrandCode();
		String cateCodeSupplier=product.getCateCode();
		
		Integer brandId=product.getBrandId();
		String cateId=product.getCateId();
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		
		StoreProduct updateProduct=new StoreProduct();
		updateProduct.setId(id);
		
		if(StringUtils.isNullOrBlank(productCodeStr)){
//			Date d = new Date();
//    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//    		String dateNowStr = sdf.format(d);
//			String shuijicode=StringUtils.getRandomCode(6);
			updateProduct.setProductCode(StringUtils.getBusinessCode("SP"));
		}else{
			updateProduct.setProductCode(productCodeStr);
		}
		//如果商品product为空则更新
		if(storeBrand!=null){
			String brandCode=storeBrand.getBrandCode();
			if(!StringUtils.isNullOrBlank(brandCode)&& StringUtils.isNullOrBlank(brandCodeSupplier)){
				updateProduct.setBrandCode(brandCode);
			}
		}
		String cateIdArray[]=cateId.split(",");
		StringBuffer sb=new StringBuffer();
		for(String cateid:cateIdArray){
			StoreCategory storeCategory=null;
			if(!StringUtils.isNullOrBlank(cateid)){
				storeCategory=storeCategoryService.queryByPrimaryId(Integer.valueOf(cateid));
			}
			
			if(storeCategory!=null){
				String cateCode=storeCategory.getCateCode();
				if(!StringUtils.isNullOrBlank(cateCode)){
					sb.append(cateCode+",");
				}
			}
		}
		String catestr=sb.toString();
		if(!StringUtils.isNullOrBlank(catestr)&& catestr.contains(",")){
//			updateProduct.setCateCode(catestr.substring(0, catestr.lastIndexOf(",")+1));
			if(!StringUtils.isNullOrBlank(catestr)){
				 if(catestr.charAt(catestr.length()-1) == ',') {
					 catestr = catestr.substring(0, catestr.length()-1); 	
				 }
				 updateProduct.setCateCode(catestr);
			}
		}
		storeProductService.update(updateProduct);
	}
	resVo.setResult(result);
 	return resVo;
 }
 
 
 
 @ApiOperation(value = "将storeprosupplier根据brandcateid设catecode设置值")
 @RequestMapping(value = "/storeProductsupplierbrandcateidnull", method = RequestMethod.GET)
 public ResponseVo<String> storeProductsupplierbrandcateidnull() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	Map<String,Object> map=new HashMap<String,Object>();
	List<StoreProductSupplier> list=storeProductSupplierService.getList(map);
	for(int i=0;i<list.size();i++){
		StoreProductSupplier product=list.get(i);
		logger.info("当前进行中初始化商品productcode第 "+i+"个 brandid="+product.getId());
		Integer id=product.getId();
		String productCodeStr=product.getProductCode();
		
		Integer brandId=product.getBrandId();
		String cateId=product.getCateId();
		
		String brandCodeSupplier=product.getBrandCode();
		String cateCodeSupplier=product.getCateCode();
		
		
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		
		StoreProductSupplier updateProductSupplier=new StoreProductSupplier();
		updateProductSupplier.setId(id);
		
		if(StringUtils.isNullOrBlank(productCodeStr)){
			updateProductSupplier.setProductCode(StringUtils.getBusinessCode("SP"));
		}else{
			updateProductSupplier.setProductCode(productCodeStr);
		}
		
		//如果商品product为空则更新
		if(storeBrand!=null){
			String brandCode=storeBrand.getBrandCode();
			if(!StringUtils.isNullOrBlank(brandCode) && StringUtils.isNullOrBlank(brandCodeSupplier)){
				updateProductSupplier.setBrandCode(brandCode);
			}
			
		}
		String cateIdArray[]=cateId.split(",");
		StringBuffer sb=new StringBuffer();
		for(String cateid:cateIdArray){
			StoreCategory storeCategory=null;
			if(!StringUtils.isNullOrBlank(cateid)){
				 storeCategory=storeCategoryService.queryByPrimaryId(Integer.valueOf(cateid));
			}
			
			if(storeCategory!=null){
				String cateCode=storeCategory.getCateCode();
				if(!StringUtils.isNullOrBlank(cateCode)){
					sb.append(cateCode+",");
				}
			}
		}
		String catestr=sb.toString();
		if(!StringUtils.isNullOrBlank(catestr)&& catestr.contains(",") && StringUtils.isNullOrBlank(cateCodeSupplier)){
			updateProductSupplier.setCateCode(catestr.substring(0, catestr.lastIndexOf(",")+1));
		}
		storeProductSupplierService.update(updateProductSupplier);
	}
	resVo.setResult(result);
 	return resVo;
 }
 
 
 
 @ApiOperation(value = "将storeprosupplier根据brandcateid设catecode设置值只去掉catecode中逗号")
 @RequestMapping(value = "/storeProductsupplierbrandcateidnulldeldouhao", method = RequestMethod.GET)
 public ResponseVo<String> storeProductsupplierbrandcateidnulldeldouhao() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	Map<String,Object> map=new HashMap<String,Object>();
	List<StoreProductSupplier> list=storeProductSupplierService.getList(map);
	for(int i=0;i<list.size();i++){
		StoreProductSupplier product=list.get(i);
		logger.info("当前进行中初始化商品productcode第 "+i+"个 brandid="+product.getId());
		Integer id=product.getId();
		String productCodeStr=product.getProductCode();
		
		Integer brandId=product.getBrandId();
		String cateId=product.getCateId();
		
		String brandCodeSupplier=product.getBrandCode();
		String cateCodeSupplier=product.getCateCode();
		
		
		StoreBrand storeBrand=storeBrandService.queryByPrimaryId(brandId);
		
		StoreProductSupplier updateProductSupplier=new StoreProductSupplier();
		updateProductSupplier.setId(id);
		
		if(StringUtils.isNullOrBlank(productCodeStr)){
			updateProductSupplier.setProductCode(StringUtils.getBusinessCode("SP"));
		}else{
			updateProductSupplier.setProductCode(productCodeStr);
		}
		
		//如果商品product为空则更新
		if(storeBrand!=null){
			String brandCode=storeBrand.getBrandCode();
			if(!StringUtils.isNullOrBlank(brandCode) && StringUtils.isNullOrBlank(brandCodeSupplier)){
				updateProductSupplier.setBrandCode(brandCode);
			}
			
		}
		String cateIdArray[]=cateId.split(",");
		StringBuffer sb=new StringBuffer();
		for(String cateid:cateIdArray){
			StoreCategory storeCategory=null;
			if(!StringUtils.isNullOrBlank(cateid)){
				 storeCategory=storeCategoryService.queryByPrimaryId(Integer.valueOf(cateid));
			}
			
			if(storeCategory!=null){
				String cateCode=storeCategory.getCateCode();
				if(!StringUtils.isNullOrBlank(cateCode)){
					sb.append(cateCode+",");
				}
			}
		}
		String catestr=sb.toString();
		
		if(!StringUtils.isNullOrBlank(catestr)){
			 if(catestr.charAt(catestr.length()-1) == ',') {
				 catestr = catestr.substring(0, catestr.length()-1); 	
			 }
			updateProductSupplier.setCateCode(catestr);
		}
		storeProductSupplierService.update(updateProductSupplier);
	}
	resVo.setResult(result);
 	return resVo;
 }
 
 
 
 
 
 @ApiOperation(value = "商品初始化商品详情")
 @RequestMapping(value = "/productSupplierDetailInit", method = RequestMethod.GET)
 public ResponseVo<String> productSupplierDetailInit(@RequestParam(value = "factoryId", required = true) Integer factoryId) throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	Map<String, Object> map=new HashMap<String,Object>();
	map.put("factoryId", factoryId);
	List<StoreProductSupplier> listStoreProductSupplier=storeProductSupplierService.getList(map);
	for(int m=0;m<listStoreProductSupplier.size();m++){
		System.out.println("添加当前个数 "+m);
		StoreProductSupplier productSupplier=listStoreProductSupplier.get(m);
		addStoreProductSupplierdetail(productSupplier);
	}
	resVo.setResult(result);
 	return resVo;
 }


 @ApiOperation(value = "上传图片入库")
 @RequestMapping(value = "/upimage", method = RequestMethod.GET)
 public ResponseVo<String> upimage() throws Exception{
    String result="OK";
	ResponseVo<String> resVo=new ResponseVo<String>();
	
	List<StoreProductSupplier> list=storeProductSupplierService.getListNewInit(null);
	for(int i=0;i<list.size();i++){
		StoreProductSupplier storeProductSupplier=list.get(i);
		logger.info("上传图片进行中第 "+i+"个 product= "+storeProductSupplier.getId());
		String image=storeProductSupplier.getImage();
		String sliderImage=storeProductSupplier.getSliderImage();
		String detailImg=storeProductSupplier.getDetailImg();
		
		if(!StringUtils.isNullOrBlank(image) && !image.contains("http")){
			String imageurl=null;
			if(!StringUtils.isNullOrBlank(image) && !image.contains("http")){
				imageurl=uploadOneoss(image);
			}
			String updatesliderjson=null;
			if(!StringUtils.isNullOrBlank(sliderImage)&& !sliderImage.contains("http")){
				List<String> listPathSortsliderImage=sortImage(sliderImage);
				if(listPathSortsliderImage!=null && !listPathSortsliderImage.isEmpty()){
					updatesliderjson=uploadMoreoss(listPathSortsliderImage);
				}
				
			}
			String updatedetailjson=null;
			if(!StringUtils.isNullOrBlank(detailImg)&& !detailImg.contains("http")){
				List<String> listPathSortdetailImg=sortImage(detailImg);
				if(listPathSortdetailImg!=null && !listPathSortdetailImg.isEmpty()){
					updatedetailjson=uploadMoreoss(listPathSortdetailImg);
				}
			}
			System.out.println(imageurl);
			System.out.println(updatesliderjson);
			System.out.println(updatedetailjson);
			StoreProductSupplier updateProdSupp=new StoreProductSupplier();
			updateProdSupp.setId(storeProductSupplier.getId());
			updateProdSupp.setImage(imageurl);
			updateProdSupp.setSliderImage(updatesliderjson);
			updateProdSupp.setDetailImg(updatedetailjson);
			storeProductSupplierService.update(updateProdSupp);
			
			
			
		}
		
	}
	resVo.setResult(result);
 	return resVo;
 }


private void saveSysAttachment(String name,String attDir,String sattDir,String attSize) {
	SystemAttachment systemAttachment=new SystemAttachment();
	systemAttachment.setAttDir(attDir);
	systemAttachment.setName(name);
	systemAttachment.setSattDir(sattDir);
	systemAttachment.setAttSize(attSize);
	systemAttachment.setAttType("image/jpeg");
	systemAttachment.setPid(0);
	Integer time=(int) (new Date().getTime()/1000);
	systemAttachment.setTime(time);
	systemAttachment.setImageType(3);
	systemAttachment.setModuleType(1);
	systemAttachment.setStoreId(-1);
	systemAttachment.setFactoryId(0);
	systemAttachment.setDeleted(0);
	systemAttachmentService.save(systemAttachment);
}


private String uploadOneoss(String pathStr) throws IOException {
	pathStr = pathStr.replaceAll("\\\\", "//");
	File file = new File(pathStr);
	if(file.exists()){
		String url=StorageUploadUtil.ossUploadFile(file, "jpg");
		String name=url.substring(url.lastIndexOf("/")+1);
		String attSize="0";	
		saveSysAttachment(name,url,url,attSize);
		return url;
	}else{
		return null;
	}
	
}


private String uploadMoreoss(List<String> listPathSortsliderImage) throws IOException {
	List<String> updatejson=new ArrayList<String>();
	StringBuffer sb=new StringBuffer();
	
	for(int i=0;i<listPathSortsliderImage.size();i++){
		logger.info("上传详细图片进行总第"+i+" 张");
		String slider = listPathSortsliderImage.get(i);
		slider = slider.replaceAll("\\\\", "//");
		File file = new File(slider);
		if(file.exists()){
			String url=StorageUploadUtil.ossUploadFile(file, "jpg");
			sb.append("\""+url+"\""+",");
			String name=url.substring(url.lastIndexOf("/")+1);
			String attDir=url;
			String sattDir=url;
			String attSize="0";	
			saveSysAttachment(name,attDir,sattDir,attSize);
		}
	}
	String lasturl=sb.toString();
	if(lasturl.contains(",")){
		updatejson.add(lasturl.substring(0, lasturl.lastIndexOf(",")));
	}
	return updatejson.toString();
}


private List<String> sortImage(String setailImg) {
	setailImg=setailImg.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\"", "");
	List<String> pathlist=new ArrayList<String>();
	if(!StringUtils.isNullOrBlank(setailImg)){
		String detailImageArray[]=setailImg.split(",");
		if(detailImageArray!=null && detailImageArray.length>0){
			List<Integer> listNum=new ArrayList<Integer>();
			String first=detailImageArray[0];
			first=first.substring(0, first.lastIndexOf("_")+1);
			for(int i=0;i<detailImageArray.length;i++){
				String imageStr=detailImageArray[i].trim();
				String name=imageStr.substring(imageStr.lastIndexOf("\\")+1);
				String arrayname[]=name.split("_");
				String namejpg=arrayname[1];
				String num=namejpg.substring(0, namejpg.indexOf("."));
				listNum.add(Integer.valueOf(num));
			}
			Collections.sort(listNum);
			for(int i=0;i<listNum.size();i++){
				pathlist.add(first+listNum.get(i)+".jpg");
			}
		}
	}
	return pathlist;
}





private void addStoreProductSupplierdetail(StoreProductSupplier storeProductSupplier) {
	String detailImage=storeProductSupplier.getDetailImg();
	String productCode=storeProductSupplier.getProductCode();
	Integer productId=storeProductSupplier.getId();
	if(!StringUtils.isNullOrBlank(detailImage)){
		detailImage=detailImage.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\"", "");
		String detailImageArray[]=detailImage.split(",");
		List<String> list=new ArrayList<String>();
		for(int i=0;i<detailImageArray.length;i++){
			String imageStr=detailImageArray[i].trim();
			list.add(imageStr);
		}
		Collections.sort(list);
		//4个切分一组
		List<List<String>> listcout=SplitBatch.split(list, 4);
		for(int j=0;j<listcout.size();j++){
			List<String> onelist=listcout.get(j);
			Collections.sort(onelist);
			StringBuffer sb=new StringBuffer();
			for(String ss:onelist){
				sb.append(ss);
				sb.append(",");
			}
			StoreProductDetailsSupplier storeProductDetails=new StoreProductDetailsSupplier();
			storeProductDetails.setDescription("");
			storeProductDetails.setImgs(sb.toString().substring(0, sb.lastIndexOf(",")));
			storeProductDetails.setProductCode(productCode);
			storeProductDetails.setProductId(productId);
			storeProductDetailsSupplierService.save(storeProductDetails);
		}
		
	}
}
 
 
 
 public static String testgetUnicode(String str){
		String result = "";
		char[] c = str.toCharArray();
		for(char tmp:c){	
			if(tmp > 255){  
				result += "/"+"u" + Integer.toHexString(tmp);  
		          
		    }else  
				result += tmp;
		}
		return result;
	}
 

}
